<?php
/* $Id$ */
/*
	traffic_limit.php
	part of stairway

	Copyright (C) 2009 Wang Zhongliang (wzlsh629@163.com)
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

##|+PRIV
##|*IDENT=page-traffic_limit
##|*NAME=traffic_limit page
##|*DESCR=Allow access to the 'Traffic Limit' page.
##|*MATCH=traffic_limit.php*
##|-PRIV

require_once('guiconfig.inc');

$lang = array('average' => '共享', 'weight' => '共享(高级)', 'limit' => '独占');

if (!is_array($config['dummynet']['pipe'])) {
    $config['dummynet']['pipe'] = array();
}

$a_pipe = &$config['dummynet']['pipe'];

if ($_POST) {

    $pconfig = $_POST;

    if ($_POST['apply']) {
        $retval = 0;
        config_lock();
        $retval = dummynet_configure();
        config_unlock();

        if (file_exists($d_dummynetconfdirty_path))
            unlink($d_dummynetconfdirty_path);

        $savemsg = "流量限制规则已生效。";
    }
}

if ($_GET['act'] == "del") {
        if ($a_pipe[$_GET['id']]) {
                unset($a_pipe[$_GET['id']]);
                write_config();
                touch($d_dummynetconfdirty_path);
                header("Location: traffic_limit.php");
                exit;
        }
}

if (isset($_POST['del_x'])) {
    /* delete selected rules */
    if (is_array($_POST['rule']) && count($_POST['rule'])) {
        foreach ($_POST['rule'] as $rulei) {
            unset($a_pipe[$rulei]);
        }
        write_config();
        touch($d_dummynetconfdirty_path);
        header("Location: traffic_limit.php");
        exit;
    }
}
else
{
    /* yuck - IE won't send value attributes for image buttons, while Mozilla does -
       so we use .x/.y to fine move button clicks instead... */
    unset($movebtn);
    foreach ($_POST as $pn => $pd) {
        if (preg_match("/move_(\d+)_x/", $pn, $matches)) {
            $movebtn = $matches[1];
            break;
        }
    }
    /* move selected rules before this rule */
    if (isset($movebtn) && is_array($_POST['rule']) && count($_POST['rule'])) {
        $a_pipe_new = array();

        /* copy all rules < $movebtn and not selected */
        for ($i = 0; $i < $movebtn; $i++) {
            if (!in_array($i, $_POST['rule']))
                $a_pipe_new[] = $a_pipe[$i];
        }

        /* copy all selected rules */
        for ($i = 0; $i < count($a_pipe); $i++) {
            if ($i == $movebtn)
                continue;
            if (in_array($i, $_POST['rule']))
                $a_pipe_new[] = $a_pipe[$i];
        }

        /* copy $movebtn rule */
        if ($movebtn < count($a_pipe))
            $a_pipe_new[] = $a_pipe[$movebtn];

        /* copy all rules > $movebtn and not selected */
        for ($i = $movebtn+1; $i < count($a_pipe); $i++) {
            if (!in_array($i, $_POST['rule']))
                $a_pipe_new[] = $a_pipe[$i];
        }

        $a_pipe = $a_pipe_new;
        write_config();
        touch($d_dummynetconfdirty_path);
        header("Location: traffic_limit.php");
        exit;
    }
}

$pgtitle = array("流量管理", "流量限制");
$pgmenu = array('traffic', '/traffic_limit.php');
$closehead = false;

include("head.inc");

echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/domLib.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/domTT.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/behaviour.js\"></script>";
echo "<script type=\"text/javascript\" language=\"javascript\" src=\"/javascript/domTT/fadomatic.js\"></script>";
?>
</head>
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc");?>
<form action="traffic_limit.php" method="post">
<?php if ($savemsg) print_info_box($savemsg); ?>
<?php if (file_exists($d_dummynetconfdirty_path)): ?><p>
<?php print_info_box_np("流量限制规则已更改。<br>请应用更改使其生效。");?><br>
<?php endif; ?>
<script type="text/javascript" language="javascript" src="/javascript/row_toggle.js">
</script>
<div class="soft_cont_right_table">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
		<td>
            <div id="mainarea">
	             <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
	                <tr id="frheader">
	                  <td width="3%" class="list">&nbsp;</td>
	                  <td width="15%" class="listhdrr">上行带宽</td>
                      <td width="15%" class="listhdrr">下行带宽</td>
                      <td width="15%" class="listhdrr">限速类型</td>
                      <td width="20%" class="listhdrr">限速对象</td>
                      <td width="10%" class="listhdrr">目的端口</td>
	                  <td width="22%" class="listhdr">描述</td>
	                  <td width="10%" class="list">
			            <table border="0" cellspacing="0" cellpadding="1">
			               <tr>
			                <?php
			                    $nrules = 0;
			                    for ($i = 0; isset($a_pipe[$i]); $i++) {
			                        $filterent = $a_pipe[$i];
			                        $nrules++;
			                    }
			                ?>
			                <td>
			                <?php if ($nrules == 0): ?>
			                <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="删除所选" border="0"><?php else: ?>
			                <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="删除所选" onclick="return confirm('是否确认删除所选')"><?php endif; ?>
			                </td>
			                <td align="center" valign="middle"><a href="traffic_limit_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" width="17" height="17" border="0"></a></td>
			               </tr>
			            </table>
			          </td>
			        </tr>
	                <?php $nrules = 0; for ($i = 0; isset($a_pipe[$i]); $i++):
	                    $pipeent = $a_pipe[$i];
	                ?>
	               <tr valign="top" id="fr<?=$nrules;?>">
	                  <td class="listt"><input type="checkbox" id="frc<?=$nrules;?>" name="rule[]" value="<?=$i;?>" onClick="fr_bgcolor('<?=$nrules;?>')" style="margin: 0; padding: 0; width: 15px; height: 15px;"></td>
                     <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';">
                        <?php if($pipeent['up']) echo htmlspecialchars($pipeent['up'] . $pipeent['up_unit']); else echo '无限制';?>
                      </td>
                     <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';">
                        <?php if($pipeent['down']) echo htmlspecialchars($pipeent['down'] . $pipeent['down_unit']); else echo '无限制';?>
                      </td>
                     <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';">
                        <?=$lang[$pipeent['method']];?>
                      </td>
                    <?php
                    
                    //build Alias popup box
                    $span_end = "";
                    $alias_span_begin = "";
                    
                    $alias_popup = rule_popup($pipeent['ip']['address'], $pipeent['ip']['port']);
                    if($alias_popup["src"])
                    {
                        $span_end = "</U></span>";
                        $alias_span_begin = $alias_popup["src"];
                    }
                                                   
                    ?>                       
                      
                     <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" id="frd<?=$nrules;?>" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';">
                        <?php if($pipeent['method'] != 'weight'): ?>
                        <?php echo $alias_span_begin;?><?=htmlspecialchars(pprint_address($pipeent['ip']));?><?php echo $span_end;?>
                        <?php else:
                            foreach($pipeent['queue'] as $key => $queue)
                            {
                                if($key > 2)
                                {
                                    echo '...';
                                    break;
                                }
                                echo pprint_address($queue['ip']);
                                echo "<br>";
                            }
                        endif; ?>
                      </td>
                      <td class="listlr" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';" class="descr">
                        <?=$textss;?><?=$pipeent['ip']['port'] ? $pipeent['ip']['port'] : $pipeent['queue'][0]['ip']['port'];?>&nbsp;<?=$textse;?>
                      </td>
	                  <td class="listbg" onClick="fr_toggle(<?=$nrules;?>)" ondblclick="document.location='traffic_limit_edit.php?id=<?=$i;?>';" class="descr">
	                    <?=$textss;?><?=htmlspecialchars($pipeent['descr']);?>&nbsp;<?=$textse;?>
	                  </td>
	                  <td valign="middle" nowrap class="list">
	                    <table border="0" cellspacing="0" cellpadding="1">
	                    <tr>
                          <td><input name="move_<?=$i;?>" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="移动到该规则前" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"></td>
                          <td><a href="traffic_limit_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="编辑" width="17" height="17" border="0"></a></td>
                        </tr>
	                    <tr>
	                      <td><a href="traffic_limit_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" title="编辑" width="17" height="17" border="0"></a></td>
	                    </tr>
	                    <tr>
	                      <td align="center" valign="middle"><a href="traffic_limit.php?act=del&id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0" title="删除" onclick="return confirm('确认删除该规则吗？')"></a></td>
	                    </tr>
	                    </table>
	                  </td>
	                </tr>
	                
	                
	              <?php $nrules++; endfor; ?>
	              <?php if ($nrules == 0): ?>
	              <td class="listt"></td>
	              <td class="listlr" colspan="6" align="center" valign="middle">
	              <span class="gray">
	                当前没有定义任何流量限制规则。<br>
	              <br>
	                点击<a href="traffic_limit_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" border="0" width="17" height="17" align="absmiddle"></a>新增流量限制规则。</span>
	              </td>
	              <?php endif; ?>
                    <tr>
                      <td width="3%" class="list">&nbsp;</td>
                      <td width="15%" class="list"></td>
                      <td width="15%" class="list"></td>
                      <td width="15%" class="list"></td>
                      <td width="20%" class="list"></td>
                      <td width="10%" class="list"></td>
                      <td width="22%" class="list"></td>
                      <td width="10%" class="list">
                        <table border="0" cellspacing="0" cellpadding="1">
                            <tr>
                              <td>
                              <?php if ($nrules == 0): ?><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_left_d.gif" width="17" height="17" title="移动到底部" border="0"><?php else: ?><input name="move_<?=$i;?>" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_left.gif" width="17" height="17" title="移动到底部" onMouseOver="fr_insline(<?=$nrules;?>, true)" onMouseOut="fr_insline(<?=$nrules;?>, false)"><?php endif; ?></td>
                              <td></td>
                            </tr>                        
                           <tr>
                            <td>
                            <?php if ($nrules == 0): ?>
                            <img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x_d.gif" width="17" height="17" title="删除所选" border="0"><?php else: ?>
                            <input name="del" type="image" src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" title="删除所选" onclick="return confirm('是否确认删除所选？')"><?php endif; ?>
                            </td>
                            <td align="center" valign="middle"><a href="traffic_limit_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" title="添加" width="17" height="17" border="0"></a></td>
                           </tr>
                        </table>
                      </td>
                    </tr>
		        </table>            
            </div>
        </td>
    </tr>
</table>
</div>
</form>
<?php include("fend.inc"); ?>
</body>
</html>
